<?php

/**
 * This is the form model class for table "cashreceiptjournalitem".
 *
 * The followings are the available columns in table 'cashreceiptjournalitem':
 * @property string $id
 * @property string $glId
 * @property string $glTransactionId
 * @property string $isDebit
 * @property string $amount
 * @property string $narration
 *
 * The followings are the available model relations:
 * @property Gltransaction $glTransaction
 * @property Cashreceiptjournal $cashReceiptJournal
 * @property Gl $gl
 */
class CashReceiptJournalItemForm extends CFormModel
{
	public $id;
    public $glId;
	public $amountDebit;
	public $amountCredit;
	public $narration;
 
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('glId, amountDebit, amountCredit', 'required'),
			array('id, glId', 'length', 'max'=>10),
			array('amountDebit', 'validateAmountDebit'),
			array('narration', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, glId, amount, narration', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'glId' => 'Gl',
			'amountDebit' => 'Debit',
			'amountCredit' => 'Credit',
			'narration' => 'Narration',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('glId',$this->glId,true);
		$criteria->compare('amountDebit',$this->amountDebit,true);
		$criteria->compare('amountCredit',$this->amountCredit,true);
		$criteria->compare('narration',$this->narration,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function validateAmountDebit($attribute, $params)
	{
		
		if (empty($this->amountDebit) && empty($this->amountCredit) )
		{
			$this->addError($attribute, 'atleast one amount (debit/credit) should not be empty or zero.');
			return false;
		}
	}
}
